package com.gaga.common.result;

import com.gaga.common.enums.CodeEnum;

import lombok.Getter;
import lombok.Setter;

public class Result<T> {


    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = CodeEnum.SUCCESS.getCode();

    /**
     * 失败
     */
    public static final int FAIL = CodeEnum.FAIL.getCode();

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T result;

    public static <T> Result<T> ok() {
        return restResult(null, SUCCESS, null);
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, SUCCESS, null);
    }
    public static <T> Result<T> ok(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Result<T> fail() {
        return restResult(null, FAIL, null);
    }

    public static <T> Result<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> Result<T> fail(T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> Result<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> Result<T> fail(Integer code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> Result<T> restResult(T data, Integer code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setResult(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}
